﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PauloC.DTO;
using PauloC.BLL;

namespace PauloC.UI.Controllers.Administracao
{
    public class CategoriaController : Controller
    {
        //
        // GET: /Categoria/

        public ActionResult Index()
        {
            List<CategoriaDTO> categorias = new CategoriaBLL().Listar();
            return View(categorias);
        }

        //
        // GET: /Categoria/Details/5

        public ActionResult Details(int id)
        {
            CategoriaDTO cat = new CategoriaBLL().Buscar(id);
            return View(cat);
        }

        //
        // GET: /Categoria/Create

        public ActionResult Create()
        {
            return View(new CategoriaDTO());
        }

        //
        // POST: /Categoria/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                CategoriaDTO cat = new CategoriaDTO();
                cat.Nome = collection["Nome"];
                CategoriaBLL catBLL = new CategoriaBLL();
                cat = catBLL.Inserir(cat);
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Categoria/Edit/5

        public ActionResult Edit(int id)
        {
            CategoriaDTO cat = new CategoriaBLL().Buscar(id);
            return View(cat);
        }

        //
        // POST: /Categoria/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                CategoriaDTO cat = new CategoriaDTO();
                cat.Nome = collection["Nome"];
                cat.Id = int.Parse(collection["Id"]);
                CategoriaBLL catBLL = new CategoriaBLL();
                catBLL.Atualizar(cat);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Categoria/Delete/5

        public ActionResult Delete(int id)
        {
            CategoriaDTO cat = new CategoriaBLL().Buscar(id);
            return View(cat);
        }

        //
        // POST: /Categoria/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                new CategoriaBLL().Excluir(int.Parse(collection["Id"]));
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
